import type {
  AssistantProps,
  AssistantStepType,
  OriginAssistantType,
  Platform,
  TableListType,
} from '@/type'

import allList from './config.json'

import articles from './articles.json'

import { Marked } from 'marked'
//
// const docs = import.meta.glob('./docs/**/*', {
//   import: 'default',
//   // eager: true,
//   query: '?raw',
// })

// async function generate() {
//   for (const [key, value] of Object.entries(docs)) {
//     //   const rootPath = key.replace('meta.json', '')
//     console.log('key', key)
//     if (key.endsWith('.md')) {
//       console.log('value', await value())
//     }
//   }
// }

// console.log('docs', docs)
// query: '?raw',
// import: 'default',
// eager: true,
// })

// console.log(docs.forEach((doc) => {
// })
//
// const marked = new Marked()
//
//   const result = import.meta.glob(rootPath, {
//     raw: true,
//     import: 'default',
//     eager: true,
//   })
// }
//
// await generate()

// 实际展示的文章顺序在这里调整
export const displayOrder: Array<string> = allList as unknown as Array<string>

let _AssistantMaps: { [key: string]: OriginAssistantType } = {
  HowToAccessAppData: {
    title: '管理应用数据',
    introduction: '从懒猫网盘中访问所有应用数据吧, 自己的数据自己做主',
    thumbnail: {
      pc: 'HowToAccessAppData/PC/Step_1/step1.gif',
      mobile: 'HowToAccessAppData/Thumbnail/media.png',
    },
    tableList: {
      PC: [
        {
          media: 'HowToAccessAppData/PC/Step_1/step1.gif',
          article: 'HowToAccessAppData/PC/Step_1/article.md',
        },
      ],
    },
  },
  HowToMountDiskToLocal: {
    title: '将网盘挂载到本地',
    introduction: '懒猫网盘支持多种协议的挂载~',
    thumbnail: {
      pc: 'HowToMountDiskToLocal/PC/step1.png',
      mobile: 'HowToMountDiskToLocal/Thumbnail/media.png',
    },
    tableList: {
      PC: [
        {
          media: 'HowToMountDiskToLocal/PC/step1.png',
          article: 'HowToMountDiskToLocal/PC/step1.md',
        },
        {
          media: 'HowToMountDiskToLocal/PC/step2.png',
          article: 'HowToMountDiskToLocal/PC/step2.md',
        },
        // {
        //   media: 'HowToMountDiskToLocal/PC/Step_3/media.png',
        //   article: 'HowToMountDiskToLocal/PC/Step_3/article.md',
        // },
        // {
        //   media: 'HowToMountDiskToLocal/PC/Step_4/media.png',
        //   article: 'HowToMountDiskToLocal/PC/Step_4/article.md',
        // },
        // {
        //   media: 'HowToMountDiskToLocal/PC/Step_5/media.png',
        //   article: 'HowToMountDiskToLocal/PC/Step_5/article.md',
        // },
        // {
        //   media: 'HowToMountDiskToLocal/PC/Step_6/media.png',
        //   article: 'HowToMountDiskToLocal/PC/Step_6/article.md',
        // },
        // {
        //   media: 'HowToMountDiskToLocal/PC/Step_7/media.png',
        //   article: 'HowToMountDiskToLocal/PC/Step_7/article.md',
        // },
        // {
        //   media: 'HowToMountDiskToLocal/PC/Step_8/media.png',
        //   article: 'HowToMountDiskToLocal/PC/Step_8/article.md',
        // },
      ],
    },
  },
  HowToSubmitApplication: {
    title: '怎么提交应用？',
    introduction: '构建应用，提交审核~',
    thumbnail: {
      pc: 'HowToSubmitApplication/Thumbnail/thumbnail.png',
      mobile: 'HowToSubmitApplication/Thumbnail/thumbnail.png',
    },
    tableList: {
      PC: [
        {
          media: 'HowToSubmitApplication/PC/Step_2/media.png',
          article: 'HowToSubmitApplication/PC/Step_2/article.md',
        },
        {
          media: 'HowToSubmitApplication/PC/Step_3/media.png',
          article: 'HowToSubmitApplication/PC/Step_3/article.md',
        },
        {
          media: 'HowToSubmitApplication/PC/Step_4/media.png',
          article: 'HowToSubmitApplication/PC/Step_4/article.md',
        },
        {
          media: 'HowToSubmitApplication/PC/Step_5/media.png',
          article: 'HowToSubmitApplication/PC/Step_5/article.md',
        },
        {
          media: 'HowToSubmitApplication/PC/Step_6/media.png',
          article: 'HowToSubmitApplication/PC/Step_6/article.md',
        },
        {
          media: 'HowToSubmitApplication/PC/Step_7/media.png',
          article: 'HowToSubmitApplication/PC/Step_7/article.md',
        },
      ],
    },
  },
  如何上传日志: {
    title: '如何上传日志？',
    introduction:
      '当您遇到异常情况时，上传日志可以帮助售后工程师帮您最快解决问题',
    thumbnail: {
      pc: '如何上传日志/PC/step1.gif',
      mobile: '如何上传日志/Thumbnail/thumbnail.png',
    },
    tableList: {
      Android: [
        {
          media: '如何上传日志/Android/Step_1/media.png',
          article: '如何上传日志/Android/Step_1/article.md',
        },
        {
          media: '如何上传日志/Android/Step_2/media.png',
          article: '如何上传日志/Android/Step_2/article.md',
        },
      ],
      iOS: [
        {
          media: '如何上传日志/iOS/Step_1/media.png',
          article: '如何上传日志/iOS/Step_1/article.md',
        },
      ],
      PC: [
        {
          media: '如何上传日志/PC/step1.gif',
          article: '如何上传日志/PC/step1.md',
        },
      ],
    },
  },
  mountOtherNAS: {
    title: '集成第三方NAS资源',
    introduction: '轻松挂载其他NAS至懒猫微服, 让懒猫微服成为您的资源中心',
    thumbnail: {
      pc: '挂载其他NAS至懒猫/PC/step1.gif',
      mobile: '挂载其他NAS至懒猫/Thumbnail/thumbnail.png',
    },
    tableList: {
      Android: [
        {
          media: '挂载其他NAS至懒猫/Android/Step_1/media.png',
          article: '挂载其他NAS至懒猫/Android/Step_1/article.md',
        },
        {
          media: '挂载其他NAS至懒猫/Android/Step_2/media.png',
          article: '挂载其他NAS至懒猫/Android/Step_2/article.md',
        },
        {
          media: '挂载其他NAS至懒猫/Android/Step_3/media.png',
          article: '挂载其他NAS至懒猫/Android/Step_3/article.md',
        },
      ],
      iOS: [
        {
          media: '挂载其他NAS至懒猫/iOS/Step_1/media.png',
          article: '挂载其他NAS至懒猫/iOS/Step_1/article.md',
        },
        {
          media: '挂载其他NAS至懒猫/iOS/Step_2/media.png',
          article: '挂载其他NAS至懒猫/iOS/Step_2/article.md',
        },
        {
          media: '挂载其他NAS至懒猫/iOS/Step_3/media.png',
          article: '挂载其他NAS至懒猫/iOS/Step_3/article.md',
        },
      ],
      PC: [
        {
          media: '挂载其他NAS至懒猫/PC/step1.gif',
          article: '挂载其他NAS至懒猫/PC/step1.md',
        },
        // {
        //   media: '挂载其他NAS至懒猫/PC/Step_2/media.png',
        //   article: '挂载其他NAS至懒猫/PC/Step_2/article.md',
        // },
      ],
    },
  },
  离线环境微服接入: {
    title: '无法连接微服怎么办？',
    introduction: '不用慌! 使用移动端设备内网直连微服',
    thumbnail: {
      pc: '离线环境微服接入/Thumbnail/thumbnail.png',
      mobile: '离线环境微服接入/Thumbnail/thumbnail.png',
    },
    tableList: {
      Android: [
        {
          media: '离线环境微服接入/Android/Step_1/media.png',
          article: '离线环境微服接入/Android/Step_1/article.md',
        },
        // {
        //   media: '离线环境微服接入/Android/Step_2/media.png',
        //   article: '离线环境微服接入/Android/Step_2/article.md',
        // },
        // {
        //   media: '离线环境微服接入/Android/Step_3/media.png',
        //   article: '离线环境微服接入/Android/Step_3/article.md',
        // },
        // {
        //   media: '离线环境微服接入/Android/Step_4/media.png',
        //   article: '离线环境微服接入/Android/Step_4/article.md',
        // },
      ],
      iOS: [
        {
          media: '离线环境微服接入/iOS/Step_1/media.png',
          article: '离线环境微服接入/iOS/Step_1/article.md',
        },
        {
          media: '离线环境微服接入/iOS/Step_2/media.png',
          article: '离线环境微服接入/iOS/Step_2/article.md',
        },
        {
          media: '离线环境微服接入/iOS/Step_3/media.png',
          article: '离线环境微服接入/iOS/Step_3/article.md',
        },
        {
          media: '离线环境微服接入/iOS/Step_4/media.png',
          article: '离线环境微服接入/iOS/Step_4/article.md',
        },
      ],
    },
  },
  HowToMakeVPNExistOnMobilePhone: {
    title: 'VPN 和代理网络模式随心切换',
    introduction: '设备已有 VPN? 懒猫微服还支持设置代理模式',
    thumbnail: {
      pc: 'HowToMakeVPNExistOnMobilePhone/Thumbnail/thumbnail.png',
      mobile: 'HowToMakeVPNExistOnMobilePhone/Thumbnail/thumbnail.png',
    },
    tableList: {
      Android: [
        {
          media: 'HowToMakeVPNExistOnMobilePhone/Android/Step_1/media.png',
          article: 'HowToMakeVPNExistOnMobilePhone/Android/Step_1/article.md',
        },
        {
          media: 'HowToMakeVPNExistOnMobilePhone/Android/Step_2/media.png',
          article: 'HowToMakeVPNExistOnMobilePhone/Android/Step_2/article.md',
        },
        {
          media: 'HowToMakeVPNExistOnMobilePhone/Android/Step_3/media.png',
          article: 'HowToMakeVPNExistOnMobilePhone/Android/Step_3/article.md',
        },
      ],
      iOS: [
        {
          media: 'HowToMakeVPNExistOnMobilePhone/iOS/Step_1/media.png',
          article: 'HowToMakeVPNExistOnMobilePhone/iOS/Step_1/article.md',
        },
        {
          media: 'HowToMakeVPNExistOnMobilePhone/iOS/Step_2/media.png',
          article: 'HowToMakeVPNExistOnMobilePhone/iOS/Step_2/article.md',
        },
        {
          media: 'HowToMakeVPNExistOnMobilePhone/iOS/Step_3/media.png',
          article: 'HowToMakeVPNExistOnMobilePhone/iOS/Step_3/article.md',
        },
      ],
    },
  },
  提升连接微服网络速度: {
    title: '提升连接微服网络速度',
    introduction: '如何从中继网络优化为直连网络',
    thumbnail: {
      pc: 'WhyAmISlowToConnectToTheMicroserver/PC/step1.png',
      mobile: 'WhyAmISlowToConnectToTheMicroserver/Thumbnail/thumbnail.png',
    },
    tableList: {
      Android: [
        {
          media: 'WhyAmISlowToConnectToTheMicroserver/Android/Step_1/media.png',
          article:
            'WhyAmISlowToConnectToTheMicroserver/Android/Step_1/article.md',
        },
        {
          media: 'WhyAmISlowToConnectToTheMicroserver/Android/Step_2/media.png',
          article:
            'WhyAmISlowToConnectToTheMicroserver/Android/Step_2/article.md',
        },
        {
          media: 'WhyAmISlowToConnectToTheMicroserver/Android/Step_3/media.png',
          article:
            'WhyAmISlowToConnectToTheMicroserver/Android/Step_3/article.md',
        },
      ],
      iOS: [
        {
          media: 'WhyAmISlowToConnectToTheMicroserver/iOS/Step_1/media.png',
          article: 'WhyAmISlowToConnectToTheMicroserver/iOS/Step_1/article.md',
        },
        {
          media: 'WhyAmISlowToConnectToTheMicroserver/iOS/Step_2/media.png',
          article: 'WhyAmISlowToConnectToTheMicroserver/iOS/Step_2/article.md',
        },
        {
          media: 'WhyAmISlowToConnectToTheMicroserver/iOS/Step_3/media.png',
          article: 'WhyAmISlowToConnectToTheMicroserver/iOS/Step_3/article.md',
        },
      ],
      PC: [
        {
          media: 'WhyAmISlowToConnectToTheMicroserver/PC/step1.png',
          article: 'WhyAmISlowToConnectToTheMicroserver/PC/step1.md',
        },
        {
          media: 'WhyAmISlowToConnectToTheMicroserver/PC/step2.png',
          article: 'WhyAmISlowToConnectToTheMicroserver/PC/step2.md',
        },
      ],
    },
  },
  HowDoIInviteUsersHowDoIOpenASubAccount: {
    title: '邀请成员',
    introduction: '了解如何邀请其他成员加入微服',
    thumbnail: {
      pc: 'HowDoIInviteUsersHowDoIOpenASubAccount/Thumbnail/thumbnail.png',
      mobile: 'HowDoIInviteUsersHowDoIOpenASubAccount/Thumbnail/thumbnail.png',
    },
    tableList: {
      PC: [
        {
          media: 'HowDoIInviteUsersHowDoIOpenASubAccount/PC/step1.gif',
          article: 'HowDoIInviteUsersHowDoIOpenASubAccount/PC/step1.md',
        },
        {
          media: 'HowDoIInviteUsersHowDoIOpenASubAccount/PC/step2.png',
          article: 'HowDoIInviteUsersHowDoIOpenASubAccount/PC/step2.md',
        },
      ],
      Android: [
        {
          media:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_1/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_1/article.md',
        },
        {
          media:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_2/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_2/article.md',
        },
        {
          media:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_3/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_3/article.md',
        },
        {
          media:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_4/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_4/article.md',
        },
        {
          media:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_5/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_5/article.md',
        },
        {
          media:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_6/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/Android/Step_6/article.md',
        },
      ],
      iOS: [
        {
          media: 'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_1/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_1/article.md',
        },
        {
          media: 'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_2/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_2/article.md',
        },
        {
          media: 'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_3/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_3/article.md',
        },
        {
          media: 'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_4/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_4/article.md',
        },
        {
          media: 'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_5/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_5/article.md',
        },
        {
          media: 'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_6/media.png',
          article:
            'HowDoIInviteUsersHowDoIOpenASubAccount/iOS/Step_6/article.md',
        },
      ],
    },
  },
  OnTheAlbumTimelineTheDifferenceBetweenTheThreeLogos: {
    title: '认识相册时间线',
    introduction: '区分本地和云端资源',
    thumbnail: {
      pc: 'OnTheAlbumTimelineTheDifferenceBetweenTheThreeLogos/Thumbnail/thumbnail.png',
      mobile:
        'OnTheAlbumTimelineTheDifferenceBetweenTheThreeLogos/Thumbnail/thumbnail.png',
    },
    tableList: {
      Android: [
        {
          media:
            'OnTheAlbumTimelineTheDifferenceBetweenTheThreeLogos/Android/Step_1/media.png',
          article:
            'OnTheAlbumTimelineTheDifferenceBetweenTheThreeLogos/Android/Step_1/article.md',
        },
      ],
      iOS: [
        {
          media:
            'OnTheAlbumTimelineTheDifferenceBetweenTheThreeLogos/iOS/Step_1/media.png',
          article:
            'OnTheAlbumTimelineTheDifferenceBetweenTheThreeLogos/iOS/Step_1/article.md',
        },
      ],
      PC: [
        {
          media:
            'OnTheAlbumTimelineTheDifferenceBetweenTheThreeLogos/PC/Step_1/media.png',
          article:
            'OnTheAlbumTimelineTheDifferenceBetweenTheThreeLogos/PC/Step_1/article.md',
        },
      ],
    },
  },
}
_AssistantMaps = articles as unknown as { [key: string]: OriginAssistantType }

function resort() {
  // check user agent
  //
  let prorities: (keyof TableListType)[] = []
  if (isIos()) {
    prorities = ['iOS', 'Android', 'PC']
  } else if (isAndroid()) {
    prorities = ['Android', 'iOS', 'PC']
  } else {
    prorities = ['PC', 'Android', 'iOS']
  }

  const _AssistantList: AssistantProps[] = displayOrder.map((key) => {
    const item = _AssistantMaps[key]
    if (!item) {
      throw new Error(`Assistant item with key "${key}" not found.`)
    }

    item.thumbnail = {
      pc: import.meta.env.BASE_URL + item.thumbnail.pc,
      mobile: import.meta.env.BASE_URL + item.thumbnail.mobile,
    }

    Object.keys(item.tableList).forEach((platform) => {
      const result = item.tableList[platform as Platform]!
      item.tableList[platform as Platform] = result.map((step) => {
        return {
          media: import.meta.env.BASE_URL + step.media,
          article: import.meta.env.BASE_URL + step.article,
        }
      })
    })
    return {
      ...item,
      key,
      contentList: [],
    }
  })

  _AssistantList.forEach((item, index) => {
    let contentList: AssistantStepType[] = []
    prorities.forEach((platform) => {
      if (item.tableList[platform]) {
        contentList = contentList.concat(item.tableList[platform])
      }
    })
    item.contentList = contentList
  })
  return _AssistantList
}

export function isIos() {
  return /iPhone|iPad|iPod/i.test(navigator.userAgent)
}

export function isAndroid() {
  return /Android/i.test(navigator.userAgent)
}

export function getPlatformAssistantList(platform: keyof TableListType) {
  // check user agent
  //
  const prorities = [platform]

  const _AssistantList: AssistantProps[] = displayOrder.map((key) => {
    const item = _AssistantMaps[key]
    if (!item) {
      throw new Error(`Assistant item with key "${key}" not found.`)
    }
    return {
      ...item,
      key,
      contentList: [],
    }
  })

  _AssistantList.forEach((item: AssistantProps) => {
    let contentList: AssistantStepType[] = []
    prorities.forEach((platform) => {
      if (item.tableList[platform]) {
        contentList = item.tableList[platform]
      } else {
        contentList = [] // 每次切换章节会加载默认平台，此处置空
      }
    })
    item.contentList = contentList
  })
  return _AssistantList
}

export const AssistantList = resort()
